package close.mazegame.screens;

import java.util.Random;

import close.mazegame.InputHandler;
import close.mazegame.MazeGame;
import close.mazegame.input.uiinput.UIInput.UIKeys;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;

public class LevelCompleteScreen extends Screen {
	
	private final GameScreen parent;
	private int select = 0;
	private String[] menuItems = { "NEXT LEVEL", "RETURN TO MENU", "EXIT GAME"};
	private static final String[] messages = {"YEAH! LEVEL COMPLETE!", "LEVEL COMPLETE, GOOD FOR YOU"};
	private final int messageNumber;
	
	private Random random = new Random();

	public LevelCompleteScreen(GameScreen parent){
		this.parent = parent;
		messageNumber = random.nextInt(messages.length);
	}
	
	@Override
	public void render() {
		parent.render();
		
		spriteBatch.begin();
		
		drawString(messages[messageNumber], MazeGame.WIDTH / 2, MazeGame.HEIGHT / 2 + 128, 4, true, new Color(1, 1, 1, 1));

		for (int i = 0; i < menuItems.length; i++) {
			if (i == select) {
				drawString("- " + menuItems[i], MazeGame.WIDTH / 2, MazeGame.HEIGHT / 2 - (i * 24), 2);
			} else {
				drawString(menuItems[i], MazeGame.WIDTH / 2, MazeGame.HEIGHT / 2 - (i * 24), 2);
			}
		}
		spriteBatch.end();
	}

	@Override
	public void update(float delta) {
		parent.updateLevelComplete(delta);
		
		if(InputHandler.uiInput.isTyped(UIKeys.UI_UP)){
			select--;
		}
		if(InputHandler.uiInput.isTyped(UIKeys.UI_DOWN)){
			select++;
		}
		while(select < 0){
			select += menuItems.length;
		}
		select = select%menuItems.length;
		
		if(InputHandler.uiInput.isTyped(UIKeys.UI_ENTER)){
			if(select == 0){
				parent.nextLevel();
				setScreen(parent);
			} else if(select == 1){
				parent.dispose();
				setScreen(new MenuScreen());
			} else if(select == 2){
				Gdx.app.exit();
			}
		}
		
		if(InputHandler.uiInput.isTyped(UIKeys.UI_ESCAPE)){
			setScreen(parent);
			return;
		}

	}

	@Override
	public boolean isOverlay() {
		return true;
	}

}
